<template>
  <div class="login-container">
    <div class="login-card">
      <div class="brand-container">
        <h2 class="brand-title">CFMOTO</h2>
      </div>
      <h1>欢迎登录</h1>
      <form @submit.prevent="login">
        <div class="form-group">
          <label for="username">用户名</label>
          <div class="input-container">
            <i class="fas fa-user"></i>
            <input type="text" id="username" v-model="formData.username" required placeholder="请输入用户名">
          </div>
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-container">
            <i class="fas fa-lock"></i>
            <input type="password" id="password" v-model="formData.password" required placeholder="请输入密码">
          </div>
        </div>
        <button type="submit">登录</button>
        <div class="register-link">
          还没有账号？<router-link to="/register">立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  name: 'Login',
  setup() {
    const formData = ref({
      username: '',
      password: ''
    })

    const login = () => {
      const users = JSON.parse(localStorage.getItem('users') || '[]')
      const user = users.find(u => u.username === formData.value.username && u.password === formData.value.password)
      if (user) {
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('currentUser', formData.value.username)
        alert('登录成功！')
        window.location.href = '/'
      } else {
        alert('用户名或密码错误！')
      }
    }

    return {
      formData,
      login
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: var(--background-color);
  padding: 20px;
}

.login-card {
  background: white;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  animation: fadeIn 0.5s ease-out;
}

.brand-container {
  text-align: center;
  margin-bottom: 30px;
}

.brand-title {
  color: var(--primary-color);
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  margin: 0;
}

h1 {
  color: var(--primary-color);
  text-align: center;
  font-size: 24px;
  margin-bottom: 30px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 25px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: var(--primary-color);
  font-weight: 500;
  font-size: 14px;
}

.input-container {
  position: relative;
}

.input-container i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--primary-color);
}

.form-group input {
  width: 100%;
  padding: 12px 15px 12px 35px;
  border: 2px solid #E8E8E8;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.form-group input:focus {
  border-color: var(--primary-color);
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 120, 160, 0.1);
}

button {
  width: 100%;
  padding: 12px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: var(--primary-dark);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.register-link a {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }
}
</style>